package com.zhongke.cotrun.controller;

import com.aliyuncs.exceptions.ClientException;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.common.util.PhoneMessageUtil;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.model.resultpackage.ReturnResult;
import com.zhongke.cotrun.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/message")
@Validated
public class MessageController {
    @Autowired
    MessageService messageService;

    /**
     * 获取验证码
     *
     * @param telephoneNumber
     * @param session
     * @return
     * @throws ClientException
     * @throws MyException
     */
    @RequestMapping(value = "/sendMessage")
    public APIResponse sendMessage(
            @NotNull(message = "电话号码不能为空")
            String telephoneNumber,
            HttpSession session,
            @RequestParam(required = false) String reset
    ) throws ClientException, MyException {
        String code = PhoneMessageUtil.getNumber(6);
        if (messageService.sendMessage(telephoneNumber, code, session,reset)) {
            return APIResponse.returnSuccess();
            //return ReturnResult.success();
        }
        return APIResponse.returnFail(CodeMsg.SED_MESSAGE_ERROP);
        // return ReturnResult.erro(CodeMsg.MESSAGE_ERROR);
    }

    /**
     * z专家发送通知
     * @param telephoneNumber
     * @param phone
     * @return
     * @throws ClientException
     */
    @RequestMapping(value = "/sendMessageForExpert")
    public APIResponse sendMessageForExpert(
            @NotNull(message = "要发送的手机号不能为空")
            String telephoneNumber,
            @NotNull(message = "联系的手机号不能为空")
            String phone,HttpSession session) throws ClientException, MyException {
        if (messageService.sendMessageForExpert(telephoneNumber, phone,session)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.SED_MESSAGE_ERROP);
    }

    /**
     * 项目通知
     * @param telephoneNumber
     * @param phone
     * @param name
     * @param content
     * @param session
     * @return
     * @throws ClientException
     * @throws MyException
     */
    @RequestMapping("/sendMessageForProject")
    public APIResponse sendMessageForProject(
            @NotNull(message = "要发送的手机号不能为空")
            String telephoneNumber,
            @NotNull(message = "联系的手机号不能为空")
            String phone,
            @NotNull(message = "称谓不能为空")
            String name,
            @NotNull(message = "项目名不能为空")
            String content,HttpSession session) throws ClientException, MyException {
    if(messageService.sendMessageForProject(telephoneNumber, phone, name, content, session)){
        return APIResponse.returnSuccess();
    }
        return APIResponse.returnFail(CodeMsg.SED_MESSAGE_ERROP);
    }

    /**
     * 服务的通知
     * @param telephoneNumber
     * @param phone
     * @param name
     * @param content
     * @param session
     * @return
     * @throws ClientException
     * @throws MyException
     */
    @RequestMapping("/sendMessageForService")
    public APIResponse sendMessageForService(
        @NotNull(message = "要发送的手机号不能为空")
                String telephoneNumber,
        @NotNull(message = "联系的手机号不能为空")
                String phone,
        @NotNull(message = "称谓不能为空")
                String name,
        @NotNull(message = "服务名不能为空")
                String content,HttpSession session) throws ClientException, MyException {
    if (messageService.sendMessageForService(telephoneNumber,phone,name,content,session)) {
        return APIResponse.returnSuccess();
    }
    return APIResponse.returnFail(CodeMsg.SED_MESSAGE_ERROP);
}


}
